<?php
namespace Common\Model;
use Think\Model;

class AdModel extends Model
{

	public function __construct()
	{
		parent::__construct();
		$this->adUrl = U('info/jump/js_event', array('aid' => '[id]'), '.html', true);
	}

	public $_validate = array();

	protected $trueTableName = 'ad';

	public function create($isAdd = true)
	{
		$type = I('post.type');
		$name = I('post.adname');
		if (empty($name)) {
			$this->error = '广告名称不能为空';
			return false;
		}
		if ($type == '' && empty(I('code'))) {
			$this->error = '广告代码不能为空';
			return false;
		}
		$data = I('post.');
		if (I('timeset') == 1) {
			if (empty(I('starttime')) || empty(I('endtime'))) {
				$this->error = '请选择广告有效期起止时间';
				return false;
			}
			$data['starttime'] = strtotime(I('starttime'));
			$data['endtime'] = strtotime(I('endtime'));
		}

		if ($type == 0) {                //代码
			$data['normbody'] = $data['type0']['code'];
		} else if ($type == 1) {        //文字
			$word = $data['type1']['word'];
			$href = $data['type1']['href'];
			$data['url'] = $href;
			if (empty($word) || empty($href)) {
				$this->error = '文字和链接不能为空';
				return false;
			}
			$tagStr = '<a href="[%url%]" target="_blank">[%words%]</a>';
			$data['normbody'] = $tagStr;
		} else if ($type == 2) {        //图片
			$img = $data['type2']['img'];
			$href = $data['type2']['href'];
			$data['url'] = $href;
			if (empty($img) || empty($href)) {
				$this->error = '图片和链接不能为空';
				return false;
			}
			$width = $data['type2']['width'];
			$height = $data['type2']['height'];

			$tagStr = '<a href="[%url%]" target="_blank"><img src="[%img%]" alt="[%words%]" width="[%width%]" height="[%height%]"></a>';

			$data['width'] = $width;
			$data['height'] = $height;
			$data['file_path'] = $img;
			$data['normbody'] = $tagStr;
		} else if ($type == 3) {        //flash
			$flash = $data['type3']['flash'];
			$href = $data['type3']['href'];
			$data['url'] = $href;
			if (empty($flash) || empty($href)) {
				$this->error = 'flash和链接不能为空';
				return false;
			}
			$width = $data['type3']['width'];
			$height = $data['type3']['height'];

			$tagStr = '<embed src="[%url%]" quality=high pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width="[%width%]"  height="[%height%]" wmode="transparent"></embed>';

			$data['width'] = $width;
			$data['height'] = $height;
			$data['file_path'] = $flash;
			$data['normbody'] = $tagStr;
		}
		if ($isAdd) {
			$res = $this->add($data);
		} else {
			$data['id'] = I('get.id');
			$res = $this->save($data);
		}
		if ($res) {
			return $res;
		} else {
			$this->error = "添加失败";
			return false;
		}
	}

	public function getAdById($id)
	{
		$map['id'] = $id;
		return $this->where($map)->find();
	}

	public function getList()
	{
		$p = I('p', 0, 'int');
		$limit = C('DEFALUT_PAGE_SIZE');
		$startNum = $p > 0 ? ($p - 1) * $limit : 0;
		$count = $this->field("count(*) as count")->find();
		$data = $this->limit($startNum, $limit)->select();
		return array(
			'count' => $count['count'],
			'data' => $data
		);
	}
}
